﻿var path = require('path');
var webpack = require("webpack");
var merge = require('webpack-merge');//合并config文件
var ExtractTextPlugin = require("extract-text-webpack-plugin");//提取打包单独的css
var HtmlWebpackPlugin = require('html-webpack-plugin');//生成html
var CleanPlugin = require('clean-webpack-plugin');//清空发布目录
//var OpenBrowserPlugin = require('open-browser-webpack-plugin');//自动打开浏览器
var currentTarget = process.env.npm_lifecycle_event

var debug;        // 是否是调试

if (currentTarget == "dev") { // 开发模式
    debug = true;
}
var PATHS = {
    // 发布目录
    publicPath: '/',
    // src 资源目录
    srcPath:  path.resolve(__dirname, 'src'),
    //node_modules path
    node_modulesPath: path.resolve('./node_modules'),
}
var entry = {
	main:path.resolve(PATHS.srcPath, 'app.js')
};
var output = {
		// 输出路径是
        path: path.join(__dirname, 'dist'),
        // publicPath: 'http://127.0.0.1:8080/',
        publicPath:PATHS.publicPath,
        filename: 'js/[name].[hash:5].js',
        //按需加载模块时输出的文件名称
    	chunkFilename: 'js/[name]-[chunkhash:8].js'
};
var resolve = {
		extensions: ['', '.js', '.vue'],
        alias:{
        	'vue': 'vue/dist/vue.js'
        }
}
var loaders = [
			{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
			{ test: /\.vue$/, loader: 'vue' },
			{
				test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
				loader: 'url',
				query: {
					limit: 10000,
					name: path.posix.join(__dirname, 'public', '[name].[hash:7].[ext]')
				}
			},
			{ test:/\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},
		      //图片文件使用 url-loader 来处理，小于8kb的直接转为base64
		      {
		      	test: /\.(jpg|jpe?g|png|gif|svg)$/,
		      	loader: 'url',
		      	query: {
		      		limit: 10000,
		      		name: 'imgs/[name].[ext]'
		      	},
		      	exclude: /node_modules/,
		      	include: path.resolve(__dirname, './src/imgs/')
		      },
		      {
		      	test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
		      	loader: "url",
		      	query: {
		      		name: '[name].[ext]?[hash]&mimetype=application/font-woff'
		      	}
		      },
		      {
		      	test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
		      	loader: "url",
		      	query: {
		      		name: '[name].[ext]?[hash]&mimetype=application/font-woff2'
		      	}
		      },
		      {
		      	test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
		      	loader: "url",
		      	query: {
		      		name: '[name].[ext]?mimetype=application/font-woff2'
		      	}
		      },
		      {
		      	test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
		      	loader: "url",
		      	query: {
		      		name: '[name].[ext]?mimetype=application/font-woff2'
		      	}
		      },
		      {
		      	test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
		      	loader: "url",
		      	query: {
		      		name: '[name].[ext]?mimetype=image/svg+xml'
		      	}
		      },
		      {
		      	test: /\.json(\?v=\d+\.\d+\.\d+)?$/,
		      	loader: "url",
		      	query: {
		      		name: '[name].[ext]?mimetype=application/json'
		      	}
		      }
]
//定义了一些文件夹的路径

var plugins = [
	  new webpack.optimize.DedupePlugin(),//删除重复依赖的文件
	  new webpack.optimize.OccurenceOrderPlugin(),//避免在文件不改变的情况下hash值不变化
      new CleanPlugin('dist'),
      new ExtractTextPlugin("css/[name].[hash:5].css"),
      //全局
      // new webpack.ProvidePlugin({
      // 	$: "jquery",
      // }),
      new webpack.optimize.CommonsChunkPlugin('js/common.[hash:5].js'),
      new HtmlWebpackPlugin({  // 首页 
        filename: 'index.html',
        template: path.resolve(PATHS.srcPath, 'index.html')
      })
];
if (!debug) {
    plugins.push(
        new webpack.optimize.UglifyJsPlugin({ // js、css都会压缩
            compress: {
                warnings: false
            },
            output: {
                comments: false,
            }
        })
    )
}
var common = {
    // 入口文件，path.resolve()方法，可以结合我们给定的两个参数最后生成绝对路径，最终指向的就是我们的index.js文件
    entry: entry,
    // 输出配置
    output: output,
    resolve: resolve,
    module: {loaders: loaders }, // 转化成es5的语法
    babel: {
      presets: ['es2015','stage-3'],
      plugins: ['transform-runtime']
    },
    plugins: plugins
}
if (debug) {
  var config = merge(common, {
    devServer: {
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
      stats: 'errors-only',
      host: process.env.HOST || 'localhost',
      port: process.env.PORT || '8080'
    },
    devtool: 'inline-source-map',
    plugins: [
      new webpack.HotModuleReplacementPlugin()
    ]
  })
  config.entry.main = ['webpack/hot/dev-server', config.entry.main]
  module.exports = config
}else{
	module.exports = merge(common, {})
}
